package de.tu_darmstadt.gdi1.towerdefense.ui;

import java.awt.GridLayout;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JLabel;
import javax.swing.JPanel;

import de.tu_darmstadt.gdi1.towerdefense.model.TowerDefenseModel;

public class PlayerFrame extends JPanel implements Observer {

	/**
	 * 
	 */
	private static final long serialVersionUID = -910107880295032410L;
	private TowerDefenseModel model;

	public PlayerFrame() {
		setLayout(new GridLayout(2, 3));
		setSize(600, 200);
	}

	@Override
	/**
	 * is called when an observable updates its content
	 */
	public void update(Observable arg0, Object arg1) {
		redraw();
	}

	/**
	 * method to add the game model to the playerframe
	 * 
	 * @param _model
	 */
	public void addModel(TowerDefenseModel _model) {
		model = _model;
		redraw();
	}

	/**
	 * adds the Player Frame as Observer to the models components
	 */
	public void addObserverToObservables() {
		model.player.addObserver(this);
		model.getMonsterSpawner().addObserver(this);
	}

	/**
	 * redraws the player frame
	 */
	public void redraw() {
		removeAll();
		add(new JLabel("Playername: " + model.player.getPlayerName()));
		add(new JLabel("  Playerhealth: " + model.getPlayerHealth()));
		add(new JLabel("  Playermoney: " + model.getPlayerMoney()));

		try {
			add(new JLabel(" Monster waves left: "
					+ model.getNumberOfWavesLeft()));
		} catch (NullPointerException e) {
		}
	}
}
